<template>
  <div class="profile-delete">
    <div class="title">
      注销账号
    </div>
    <el-form
      size="large"
      ref="passFormRef"
      :model="passForm"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item prop="checkPass" label="确认密码：">
        <el-input v-model="passForm.checkPass" show-password/>
      </el-form-item>
      <el-form-item style="margin-top: 3rem;">
        <el-button color="#e15958" size="large" class="button" @click="submitForm(passFormRef)">
          <span class="button_text">注销账号</span>
        </el-button>
        <el-button color="#4dcdef" size="large" class="button" @click="cancel">
          <span class="button_text">取消</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage, ElNotification } from 'element-plus';
import { ref, inject } from 'vue';
import { useRouter } from 'vue-router';
import { login, closeAccount } from '../api/api';
import { decryptDES } from '../features/Des';
import { useStore } from '../stores';

const passFormRef = ref()
const passForm = ref({
  checkPass: ''
})

// rules
const rules = ref({
  checkPass: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

// 使用路由和store
const router = useRouter()
const store = useStore()
// submit
const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      let userID = store.id
      // 使用des加密
      login(store.id).then(response => {
        // 验证密码
        let truePass = decryptDES(response.data.password)
        if (truePass === passForm.value.checkPass) {
          // 注销账号
          closeAccount(store.id).then(response => {
            reset().then(() =>
              ElNotification({
                title: '注销成功ヾ(￣▽￣)Bye~Bye~',
                message: userID + '，世界很大，我们有缘再见吧',
                type: 'success'
              })
            )
          }).catch(error => {
            console.log(error)
            ElNotification({
              title: '注销失败',
              message: error.message,
              type: 'error'
            })
          })
        } else {
          ElNotification({
            title: '密码错误',
            message: '密码错误，注销失败',
            type: 'error'
          })
        }
      }).catch(error => {
        console.log(error)
        ElNotification({
          title: '注销失败',
          message: error.message,
          type: 'error'
        })
      })
      return true
    } else {
      ElNotification({
        title: '注销失败',
        message: '请规范填写后重新提交',
        type: 'error'
      })
      return false
    }
  })
}

// 取消
const cancel = async() => {
  router.go(-1)
  ElMessage('已取消')
}

// 重置store、localStorage
const reset = async() => {
  store.$reset()
  localStorage.clear()
  await router.push({name: 'home'})
  reload()
}
// reload页面 从app.vue调用的方法
const reload = inject('Reload')
</script>

<style scoped>
.profile-delete {
  height: 100%;
  width: 100%;
}
.title {
  text-align: left;
  font-size: 1.5rem;
  line-height: 3rem;
  font-weight: 600;
  padding-bottom: 1rem;
  margin-bottom: 1rem;
}
.button {
  width: 30%;
  box-shadow: var(--el-box-shadow-light);
}
.button_text {
  font-size: .9rem;
  font-weight: 700;
  color: #fff;
}
</style>